package com.masterclient.app.businessmodule.minemodule.address.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.address.AddResultBean;
import com.eoner.baselibrary.bean.address.AddressListBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/07/31
*/
public class AddNewAddressPresenter extends AddNewAddressContract.Presenter {

    @Override
    public void addNewAddress(String a_id, String address, String default_a, String full_name, String mobile) {

        mView.baseShowCommitLoading("正在加载...");
        Observable<AddResultBean> observable = RetrofitUtil.getInstance().addAddress(a_id,address,default_a,full_name,mobile);

        Disposable disposable = toSubscribe(observable, new Consumer<AddResultBean>() {
            @Override
            public void accept(@NonNull AddResultBean bean) throws Exception {
                mView.baseHiddenCommitLoading();
                if("0".equals(bean.getCode())){
                    mView.addAddressSuccess(bean.getData());
                }else {
                    mView.addAddressFailed(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenCommitLoading();
                ToastUtil.showToast("网络异常，请重试");
            }
        });

        mRxManager.add(disposable);

    }

    @Override
    public void getRegionList(String region_id) {
        Observable<AddressListBean> observable = RetrofitUtil.getInstance().getRegionList(region_id);
        Disposable disposable = toSubscribe(observable, new Consumer<AddressListBean>() {
            @Override
            public void accept(@NonNull AddressListBean bean) throws Exception {
                if(bean!=null&&bean.getData()!=null){
                    mView.getAddressList(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}